package com.ruoyi.rgpp.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rgpp.mapper.JmrJibenstudentsMapper;
import com.ruoyi.rgpp.domain.JmrJibenstudents;
import com.ruoyi.rgpp.service.IJmrJibenstudentsService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 学生基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-13
 */
@Service
public class JmrJibenstudentsServiceImpl implements IJmrJibenstudentsService {
    @Autowired
    private JmrJibenstudentsMapper jmrJibenstudentsMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询学生基本信息
     *
     * @param id 学生基本信息主键
     * @return 学生基本信息
     */
    @Override
    public JmrJibenstudents selectJmrJibenstudentsById(Long id) {
        return jmrJibenstudentsMapper.selectJmrJibenstudentsById(id);
    }

    /**
     * 查询学生基本信息列表
     *
     * @param jmrJibenstudents 学生基本信息
     * @return 学生基本信息
     */
    @Override
    public List<JmrJibenstudents> selectJmrJibenstudentsList(JmrJibenstudents jmrJibenstudents) {
        return jmrJibenstudentsMapper.selectJmrJibenstudentsList(jmrJibenstudents);
    }

    /**
     * 新增学生基本信息
     *
     * @param jmrJibenstudents 学生基本信息
     * @return 结果
     */
    @Override
    public int insertJmrJibenstudents(JmrJibenstudents jmrJibenstudents) {
        return jmrJibenstudentsMapper.insertJmrJibenstudents(jmrJibenstudents);
    }

    /**
     * 修改学生基本信息
     *
     * @param jmrJibenstudents 学生基本信息
     * @return 结果
     */
    @Override
    public int updateJmrJibenstudents(JmrJibenstudents jmrJibenstudents) {
        return jmrJibenstudentsMapper.updateJmrJibenstudents(jmrJibenstudents);
    }

    /**
     * 批量删除学生基本信息
     *
     * @param ids 需要删除的学生基本信息主键
     * @return 结果
     */
    @Override
    public int deleteJmrJibenstudentsByIds(String ids) {
        return jmrJibenstudentsMapper.deleteJmrJibenstudentsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生基本信息信息
     *
     * @param id 学生基本信息主键
     * @return 结果
     */
    @Override
    public int deleteJmrJibenstudentsById(Long id) {
        return jmrJibenstudentsMapper.deleteJmrJibenstudentsById(id);
    }

    @Override
    public String importJibenstudents(List<JmrJibenstudents> jibenstudentList, Boolean UpdateSupport, String operName) {
        if (StringUtils.isNull(jibenstudentList) || jibenstudentList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (JmrJibenstudents JmrJibenstudents : jibenstudentList)
        {
            try
            {
                // 验证是否存在这个用户
                JmrJibenstudents e = jmrJibenstudentsMapper.selectJmrJibenstudentsById(JmrJibenstudents.getId());
                if (StringUtils.isNull(e))
                {
                    BeanValidators.validateWithException(validator, JmrJibenstudents);
                    this.insertJmrJibenstudents(JmrJibenstudents);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、导入数据 " + JmrJibenstudents.getName() + " 导入成功");
                }
                else if (UpdateSupport)
                {
                    BeanValidators.validateWithException(validator, JmrJibenstudents);
                    JmrJibenstudents.setUpdateBy(operName);
                    this.updateJmrJibenstudents(JmrJibenstudents);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、导入数据 " + JmrJibenstudents.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、导入数据 " + JmrJibenstudents.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入数据 " + JmrJibenstudents.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}




